<%@ page import="domain.Product" %>
<%@ page import="dao.ProductDao" %>
<%--
  Created by IntelliJ IDEA.
  User: ThinkPad
  Date: 2021/12/25
  Time: 2:19
  To change this template use File | Settings | File Templates.
--%>
<%-- 请勿直接访问此页面 --%>
<%@ page contentType="text/html;charset=UTF-8"%>
<html>
<head>
    <title>后台-添加商品反馈</title>
</head>
<body>
<%
    String pname = request.getParameter("pname");
    String desc = request.getParameter("desc");
    ProductDao dao = new ProductDao();
    boolean legal = true;
    // 输入的检测是不完全的，还需要对用户输入数据的合法性进行进一步检测
    if (dao.exist(pname)){
        legal = false;
%>
<script type="text/javascript">
    alert("为了良好的用户体验，请勿添加相同名字的产品。您可以尝试先修改产品信息。");
    window.location="manage.jsp";
</script>
<%
    }
    try {
        int quantity = Integer.parseInt(request.getParameter("quantity"));
        double price = Double.parseDouble(request.getParameter("price"));
        if (legal){
            Product product = new Product();
            product.setName(pname);
            product.setDescription(desc.equals("")?null:desc);
            product.setCount(quantity);
            product.setPrice(price);
            if (dao.insert(product)){
%>
<div style="font-size: larger;color: chocolate;text-align: center;">添加商品成功！</div><br />
<div style="text-align: center">
    以下商品信息：<br />
    <table style="margin-left: 35%">
        <tr>
            <td style="width: 100px">商品名：</td>
            <td><%=product.getName()%></td>
        </tr>
        <tr>
            <td>商品库存：</td>
            <td><%=product.getCount()%></td>
        </tr>
        <tr>
            <td>商品价格：</td>
            <td><%=product.getPrice()%></td>
        </tr>
        <tr>
            <td>商品描述：</td>
            <td><%=product.getDescription()==null?"暂无产品描述":product.getDescription()%></td>
        </tr>
    </table>
    <br />已经被添加进数据库。<br />
    五秒后将为您重新跳转至后台管理页面……
<%
                response.setHeader("refresh","5;URL=manage.jsp");
            }
        }
    }catch (NumberFormatException e){
        e.printStackTrace();
    }
%>
</div>
<br /><br /><br /><br />
<%@include file="tail.jsp"%>
</body>
</html>
